/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.tech15.core.dao;

import static cn.org.rapid_framework.util.ObjectUtils.isNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseHibernateDao;

import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.tech15.core.model.Module;
import com.tech15.core.vo.query.ModuleQuery;

@Repository
public class ModuleDao extends BaseHibernateDao<Module,Long>{

	public Class getEntityClass() {
		return Module.class;
	}
	
	public Page findPage(ModuleQuery query) {
        //XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
        // [column]为字符串拼接, {column}为使用占位符. [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接 
        // [column] 为PageRequest的属性
		String sql = "select t from Module t where 1=1 "
			+ "/~ and t.name = '[name]' ~/"
			+ "/~ and t.mdno = '[mdno]' ~/"
			+ "/~ and t.remark = '[remark]' ~/"
			+ "/~ and t.layer = '[layer]' ~/"
			+ "/~ and t.pid = '[pid]' ~/"
			+ "/~ and t.url = '[url]' ~/"
			+ "/~ and t.fixed = '[fixed]' ~/"
			+ "/~ order by [sortColumns] ~/";

		return pageQuery(sql,query);
	}
	/***
	 * 查询所有需要授权Layer2层模块
	 * @return
	 */
	public List<Module> findAllParentByAuthLayer2(Integer appid){
		String hql = "select t from Module t where t.layer=1 and t.type=0 and t.appid=? order by t.sortno asc";
		return getHibernateTemplate().find(hql,appid);
	}
	
	/***
	 * 查询所有需要授权Layer2层模块
	 * @return
	 */
	public List<Module> findAllParentByFixedLayer2(){
		String hql = "select t from Module t where t.layer=1 and t.fixed=1 order by t.sortno asc";
		return getHibernateTemplate().find(hql);
	}
	
	public List<Module> userFinalModuleTree(Long userid) {
		String sql = "select level,t.*,"
			       +" get_peruser(t.mdid,"+userid+") as my,"
			       +" get_pergroup(t.mdid,"+userid+") as pg,"
			       +" get_userisauth(t.mdid,"+userid+") as isauth"
			       +" from sys_module t where t.type=0 start with  t.layer=1 connect by prior t.mdid = t.pid";
		Map map = new HashMap();
		map.put("level", "Long");
		map.put("mdid", "Long");
		map.put("name", "String");
		map.put("remark", "String");
		map.put("isAuth", "Long");
		map.put("pid", "Long");
		map.put("my", "String");
		map.put("pg", "String");
		
		return findSqlAll(sql, map, Module.class);
	}

	public List<Module> findAuthModuleTree(Long authid,String authType,Integer appid) {
		String sql = "select level,t.*,(select count(*) from sys_module_auth a where a.auth_type="+authType+" and a.mdid=t.mdid and a.authid="+authid+") as isauth from sys_module t where t.appid="+appid+" and t.type=0 start with t.layer=1 connect by prior t.mdid = t.pid order by t.sortno asc";
		
		Map map = new HashMap();
		map.put("level", "Long");
		map.put("mdid", "Long");
		map.put("name", "String");
		map.put("remark", "String");
		map.put("pid", "Long");
		map.put("isAuth", "Long");
		
		return findSqlAll(sql, map, Module.class);
	}
	
	/**查询所有超级管理员模块-父*/
	public List<Module> findAllSysAdminFun(){
		String hql = "select t from Module t where t.layer=1 and t.type=-1 and t.isshfw=0 order by t.sortno asc";
		return getHibernateTemplate().find(hql);
	}
	/**查询所有客户管理员模块-父*/
	public List<Module> findAllAdminFun(){
		String hql = "select t from Module t where t.layer=1 and t.type=-2 order by t.sortno asc";
		return getHibernateTemplate().find(hql);
	}
	/**查询所有授权的模块-子*/
	public List<Module> findAllFunByUserId(Long userid){
		String sql = "select t.* from ("+
					"select t.* from sys_module t,sys_module_auth t1 where t.mdid=t1.mdid and t1.auth_type=1 and t1.authid="+userid+
					" union "+
					"select t.* from sys_module t,sys_module_auth t1,sys_group_user t2 where t.mdid=t1.mdid and t1.authid=t2.pgid and t1.auth_type=2 and t2.userid="+userid+
					") t order by t.sortno";
		Map map = new HashMap();
		map.put("mdid", "Long");
		map.put("name", "String");
		map.put("remark", "String");
		map.put("pid", "Long");
		map.put("url", "String");
		return findSqlAll(sql, map, Module.class);
	}
	/**查询所有固定模块或非固定模块-父*/
	public List<Module> findAllParentFunByUserId(Long userid,Integer appid){
		String sql = "select * from sys_module a  where a.mdid in " +
					"("+
					" 	select distinct t.pid from "+
					"	("+
					"		select t.* from sys_module t,sys_module_auth t1 where t.mdid=t1.mdid and t1.auth_type=1 and t1.authid="+userid+
					" 		union "+
					"		select t.* from sys_module t,sys_module_auth t1,sys_group_user t2 where t.mdid=t1.mdid and t1.authid=t2.pgid and t1.auth_type=2 and t2.userid="+userid+
					"	) t where t.appid=" + appid +
					") order by a.sortno asc";
		Map map = new HashMap();
		map.put("mdid", "Long");
		map.put("name", "String");
		map.put("mdno", "String");
		map.put("remark", "String");
		map.put("pid", "Long");
		map.put("url", "String");
		return findSqlAll(sql, map, Module.class);
	}
	
	public Integer getModuleCount(Long userid,Integer appid){
		/*String sql = "select count(*) from sys_module a  where a.mdid in " +
					"("+
					" 	select distinct t.pid from "+
					"	("+
					"		select t.* from sys_module t,sys_module_auth t1 where t.mdid=t1.mdid and t1.auth_type=1 and t1.authid="+userid+
					" 		union "+
					"		select t.* from sys_module t,sys_module_auth t1,sys_group_user t2 where t.mdid=t1.mdid and t1.authid=t2.pgid and t1.auth_type=2 and t2.userid="+userid+
					"	) t where t.appid=" + appid +
					") ";
		return this.getCount(sql);*/
		String sql = "select count(*) from sys_module t,sys_module_auth t1,sys_group_user t2,sys_group t3 where t.mdid=t1.mdid and t1.authid=t2.pgid and t2.pgid=t3.pgid and t1.auth_type=2 and t2.userid="+userid+" and t3.appid="+appid;
		return this.getCount(sql);
	}

	public List<Module> findAllByAppid(Integer appid) {
		String hql = "from Module t where t.type=0 and t.appid=? and t.pid is null";
		return this.getHibernateTemplate().find(hql,appid);
	}

	public List<Module> findAllByPid(Long pid) {
		String hql = "from Module t where t.pid=?";
		return this.getHibernateTemplate().find(hql,pid);
	}

}
